package com.spzx.user.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.user.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.api.domain.UserAddress;
import com.spzx.user.service.IUserAddressService;

/**
 * 用户地址Service业务层处理
 *
 * @author atguigu
 * @date 2025-03-13
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private IRegionService regionService;

    /**
     * 查询用户地址列表
     *
     * @param userId
     * @return 用户地址
     */
    @Override
    public List<UserAddress> selectUserAddressList(Long userId) {
        LambdaQueryWrapper<UserAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAddress::getUserId, userId);
        return userAddressMapper.selectList(queryWrapper);
    }

    @Override
    public int insertUserAddress(UserAddress userAddress) {

        //获取当前登录用户的id
        Long userId = SecurityContextHolder.getUserId();
        userAddress.setUserId(userId);

        //根据地区code获取地区名称
        String provinceName = regionService.getNameByCode(userAddress.getProvinceCode());
        String cityName = regionService.getNameByCode(userAddress.getCityCode());
        String districtName = regionService.getNameByCode(userAddress.getDistrictCode());

        //封装全地址
        String fullAddress = provinceName + cityName + districtName + userAddress.getAddress();
        //将完整地址设置进数据库中
        userAddress.setFullAddress(fullAddress);

        //如果当前userAddress的isDefault的值是1，那么就把其他地址修改为非默认
        if (userAddress.getIsDefault().intValue() == 1) {
            //根据id查询数据库
            LambdaQueryWrapper<UserAddress> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserAddress::getUserId, userAddress.getUserId());

            //将其他地址的isDefault设置为0
            UserAddress updateUserAddress = new UserAddress();
            updateUserAddress.setIsDefault(0);
            //由于数据是先update，然后再insert，所以这个时候数据库中还没有我们新增的地址记录
            //因此我们可以放心大胆的将列表中的地址是否为默认值修改为非默认，所以这里没有问题
            baseMapper.update(updateUserAddress, queryWrapper);

        }

        //数据插入到数据库
        return baseMapper.insert(userAddress);

    }

    @Override
    public int updateUserAddress(UserAddress userAddress) {
        //获取当前登录用户的id
        Long userId = SecurityContextHolder.getUserId();
        userAddress.setUserId(userId);

        //根据地区code获取地区名称
        String provinceName = regionService.getNameByCode(userAddress.getProvinceCode());
        String cityName = regionService.getNameByCode(userAddress.getCityCode());
        String districtName = regionService.getNameByCode(userAddress.getDistrictCode());

        //封装全地址
        String fullAddress = provinceName + cityName + districtName + userAddress.getAddress();
        //将完整地址设置进数据库中
        userAddress.setFullAddress(fullAddress);

        //如果当前userAddress的isDefault的值是1，那么就把其他地址修改为非默认
        if (userAddress.getIsDefault().intValue() == 1) {
            //根据id查询数据库
            LambdaQueryWrapper<UserAddress> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserAddress::getUserId, userAddress.getUserId());

            //将其他地址的isDefault设置为0
            //这里创建了一个新的UserAddress对象，在这个对象里面我们把所有的isDefault都设置为了0
            //所以这并不会影响原本的UserAddress对象中的值
            UserAddress updateUserAddress = new UserAddress();
            updateUserAddress.setIsDefault(0);
            baseMapper.update(updateUserAddress, queryWrapper);

        }

        //在这里我们把原本的isDefault的值(按了默认按钮，就是1)在数据库中再更新一次就可以了。
        return baseMapper.updateById(userAddress);
    }
}
